﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace GoogleMusicDesktop
{
    public partial class MainForm : Form
    {
        const int GoogleMusicMaxPageSongsCount = 20;
        public MainForm()
        {
            InitializeComponent();
            bwShowSongTime.RunWorkerAsync();
        }

        private void sbSongs_Click(object sender, EventArgs e)
        {
            string Key = sbSongs.Text.Trim();
            if (string.IsNullOrEmpty(Key))
            {
                return;
            }
            string searchString = string.Format("http://www.google.cn/music/search?q={0}&cat=song", Key);
            List<SongInfo> songCollect = new List<SongInfo>();
            
            axWMP.currentPlaylist.clear();
            lvSongList.Items.Clear();

            int startSongIndex = 0;
            do
            {
                string tmpSearchString = searchString + "&start=" + startSongIndex.ToString();
                string html = HttpHelper.GetUrlData(tmpSearchString, Encoding.UTF8, 5);
                string SongStrs = StringHelper.GetSongListString(html);
                string[] SongArray = StringHelper.GetSongListStringArray(SongStrs);
                foreach (string strsong in SongArray)
                {
                    SongInfo newSi = StringHelper.GetSongInfo(strsong);
                    songCollect.Add(newSi);

                    string downloadUrl = StringHelper.GetSongDownloadUrl(newSi.Id);
                    if (string.IsNullOrEmpty(downloadUrl))
                    {
                        MessageBox.Show("无法获取到歌曲URL地址，是否下载同一首歌曲次数过多？请更换IP或等待谷歌解封后再试。");
                        return;
                    }
                    axWMP.currentPlaylist.appendItem(axWMP.newMedia(downloadUrl));
                    lvSongList.Items.Add(String.Format("{0} - {1}", newSi.Artist, newSi.Title));
                    Application.DoEvents();
                }
                startSongIndex += GoogleMusicMaxPageSongsCount;
            } while (songCollect.Count != 0 && songCollect.Count % GoogleMusicMaxPageSongsCount == 0);
            axWMP.Ctlcontrols.play();
            lblTime.Text = axWMP.currentPlaylist.count.ToString();
        }

        private void axWMP_MediaChange(object sender, AxWMPLib._WMPOCXEvents_MediaChangeEvent e)
        {
            string songInfo = String.Format("{0} - {1}", axWMP.currentMedia.getItemInfo("Artist"), axWMP.currentMedia.name);
            lblSinger.Text = songInfo;
        }

        private void bwShowSongTime_DoWork(object sender, DoWorkEventArgs e)
        {
            while (FormHelper.IsFormExist(this))
            {
                bwShowSongTime.ReportProgress(0);
                Thread.Sleep(500);
            }
        }

        private void bwShowSongTime_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ShowSongTime();
        }

        /// <summary>
        /// 显示播放歌曲时间位置
        /// </summary>
        private void ShowSongTime()
        {
            try
            {
                if (axWMP.currentMedia == null)
                {
                    lblTime.Text = "00:00";
                }
                else
                {
                    lblTime.Text = axWMP.Ctlcontrols.currentPositionString;
                }
            }
            catch { }
        }
    }
}
